package com.cazadoresmadridejos.pdf;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import es.cazadoresmadridejos.model.BigGameAssistance;
import es.cazadoresmadridejos.model.Partner;
import es.cazadoresmadridejos.util.itext.ITextUtil;


public class FirstPdf {
	private String FILE = "c:/temp/FirstPdf.pdf";
	private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 21, Font.UNDERLINE);
	private Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD);
	private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.RED);
	private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
	private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
	private Font footer = new Font(Font.FontFamily.TIMES_ROMAN, 9, Font.BOLD);



	public static void main(String[] args) {
		for (int i = 55; i < 103; i++) {

			try {
				Document document = new Document();
				PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:/temp/FirstPdf " + i + ".pdf"));
				//writer.setPageEvent(new ITextUtil());
				document.open();
				document.setMargins(10f, 10f, 10f, 10f);
				FirstPdf util = new FirstPdf();
				System.out.println("procesando:" + i);
				util.createDocument(document, i);

				document.close(); 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void createDocument(Document document,int numberOfElements)
			throws DocumentException {
		Paragraph preface = new Paragraph();
		// We add one empty line
		// Lets write a big header
		Paragraph title = new Paragraph("Sociedad de Cazadores de Madridejos", catFont);
		title.setAlignment(Element.ALIGN_CENTER);
		preface.add(title);

		Paragraph bigGame = new Paragraph("MONTERIA", catFont);
		bigGame.setAlignment(Element.ALIGN_CENTER);
		preface.add(bigGame);
		addEmptyLine(preface, 1);

		Paragraph date = new Paragraph("Sábado 12 de Octubre de 2012", normal);
		date.setAlignment(Element.ALIGN_CENTER);
		preface.add(date);

		addEmptyLine(preface, 1);

		PdfPTable table = new PdfPTable(2);
		//table.setWidthPercentage(widthPercentage);
		java.util.List<String> list = order(mock(numberOfElements));

		float fontSize = 12;
		int newLines = 0;
		if(list.size() < 55){
			newLines = 55 - list.size();
			newLines /= 2;
		}else if(list.size() >= 55 && list.size() < 61 ){
			fontSize = 11;
		}else if(list.size() >= 61 && list.size() < 63 ){
			fontSize = 10;
		}else if(list.size() >= 63 && list.size() < 68 ){
			fontSize = 9.5f;
		}else if(list.size() >= 68 && list.size() < 71 ){
			fontSize = 9;
		}else if(list.size() >= 71 && list.size() < 73 ){
			fontSize = 8.5f;
		}else if(list.size() >= 73 && list.size() < 77 ){
			fontSize = 8;
		}else if(list.size() >= 77 && list.size() < 81 ){
			fontSize = 7.5f;
		}else if(list.size() >= 81 && list.size() < 83 ){
			fontSize = 7;
		}else if(list.size() >= 83 && list.size() < 88 ){
			fontSize = 6.5f;
		}else if(list.size() >= 88 && list.size() < 93 ){
			fontSize = 6f;
		}else if(list.size() >= 93 && list.size() < 97 ){
			fontSize = 5.5f;
		}
		for (String string : list) {
			Font forTable = new Font(Font.FontFamily.TIMES_ROMAN, fontSize, Font.BOLD);
			PdfPCell c1 = new PdfPCell(new Phrase(string,forTable));
			c1.setBorder(Rectangle.NO_BORDER);
			table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);
		}

		preface.add(table);

		addEmptyLine(preface, newLines);
		document.add(preface);
	}

	private java.util.List<BigGameAssistance> mock(int size){
		java.util.List<BigGameAssistance> list = new ArrayList<BigGameAssistance>();
		for (int i = 0; i <= size; i++) {
			BigGameAssistance bga1 = new BigGameAssistance();
			Partner p1 = new Partner();
			p1.setName("Alfonso");
			p1.setLastName("Gallego Rodríguez Garcia moreno calvo");
			bga1.setPartner(p1);
			bga1.setListIndex(i);
			list.add(bga1);		
		}
		return list;

	}
	private java.util.List<String> order(java.util.List<BigGameAssistance> bigGameAssstances){

		java.util.List<BigGameAssistance> r1 = bigGameAssstances.subList(0, bigGameAssstances.size() / 2);
		java.util.List<BigGameAssistance> r2 = bigGameAssstances.subList(bigGameAssstances.size() / 2, bigGameAssstances.size());

		java.util.List<String> result = new ArrayList<String>();

		Iterator<BigGameAssistance> it1 = r1.iterator();
		Iterator<BigGameAssistance> it2 = r2.iterator();

		for (int i = 0; i < r1.size(); i++) {
			BigGameAssistance bga1 = it1.next();
			result.add(bga1.getListIndex() + ". " + bga1.getPartner().getName() + " " + bga1.getPartner().getLastName());

			if(it2.hasNext()){
				BigGameAssistance bga2 = it2.next();
				result.add(bga2.getListIndex() + ". " + bga2.getPartner().getName() + " " + bga2.getPartner().getLastName());
			}
		}

		return result;
	}

	private void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
} 